package com.eternal.control.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.eternal.control.R;
import com.eternal.framework.utils.ConvertUtils;

/**
 * Created by Administrator
 * On 2020/5/3
 * Description:
 * @author Administrator
 */
public class CycleView extends View {
    private static final int SHADER=8;
    private int minRadius=67, midDistance=11, midStroke=40, maxDistance=26;
    private int size, center;

    private Bitmap background;
    private Paint paint;

    public CycleView(Context context) {
        this(context, null);
    }

    public CycleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CycleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CycleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }

    private void initPaint(){
//        setLayerType(LAYER_TYPE_SOFTWARE, null);
        paint=new Paint(Paint.DITHER_FLAG|Paint.ANTI_ALIAS_FLAG);
    }

    private void initBackground(){
        Paint paint=new Paint(Paint.DITHER_FLAG|Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(ConvertUtils.dp2px(1));
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        BlurMaskFilter filter = new BlurMaskFilter(SHADER, BlurMaskFilter.Blur.OUTER);
        //阴影 15f 阴影长度
        paint.setMaskFilter(filter);
        int radius=ConvertUtils.dp2px(minRadius + midDistance + midStroke + maxDistance);
        background=Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(background);
        int center=size/2;
        canvas.drawCircle(center, center, radius, paint);

        paint.setMaskFilter(null);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ConvertUtils.dp2px(40));
        LinearGradient mLinearGradient = new LinearGradient(ConvertUtils.dp2px(50), 0, size - ConvertUtils.dp2px(50), 0,
                new int[]{getResources().getColor(R.color.color_21409A), getResources().getColor(R.color.color_00B8EC)}, null, Shader.TileMode.CLAMP);
        paint.setShader(mLinearGradient);
        int stroke=ConvertUtils.dp2px(midStroke)/2;
        //半径为布局宽度一半 - 最外圆内边距 - 中圆的线宽度的一半
        radius = ConvertUtils.dp2px(minRadius + midDistance) + stroke;
        canvas.drawCircle(center, center, radius, paint);
        //画中圆的外阴影
        paint.setStrokeWidth(ConvertUtils.dp2px(2));
        paint.setColor(getResources().getColor(R.color.color_80000000));
        paint.setShader(null);
        filter = new BlurMaskFilter(20f, BlurMaskFilter.Blur.OUTER);
        paint.setMaskFilter(filter);
        canvas.drawCircle(center, center, radius + stroke + ConvertUtils.dp2px(1), paint);
        //画中圆的内阴影
        paint.setColor(getResources().getColor(R.color.color_80FFFFFF));
        paint.setStrokeWidth(ConvertUtils.dp2px(1));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(center, center, radius - stroke , paint);

        paint.setMaskFilter(null);
        paint.setColor(getResources().getColor(R.color.color_292929));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(center, center, ConvertUtils.dp2px(minRadius), paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(background!=null){
            int distance=center - background.getHeight()/2;
            canvas.drawBitmap(background, distance, distance, paint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        size=MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 控件宽、高
        size = Math.min(h, w);
        // 温度，刻度的半径
        center = size / 2;
        initBackground();
    }
}
